import random, paramiko
def ramdom_string(len):
    result = []
    for i in range(0, len):
        if i % 3 == 0:
            result.append(random.choice('1234567890'))
        if i % 3 == 1:
            result.append(random.choice('abcdefghijklmnopqrstuvwxyz'))
        if i % 3 == 2:
            result.append(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
    random.shuffle(result)
    return "".join(result)

def ssh_remote_command(addr, command):
    private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname=addr, port=22, username="root", timeout = 2, pkey=private_key)
    stdin, stdout, stderr = s.exec_command(command)
    stdout= stdout.read()
    stderr= stderr.read()
    s.close()
    return stdout+stderr

def ssh_copy_put(addr, srcfile, destfile):
    private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname=addr, port=22, username="root", timeout=2, pkey=private_key)
    sftp = s.open_sftp()
    result = sftp.put( srcfile, destfile )
    return result